//
//  YLExchangeRecordController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/11.
//  Copyright © 2017年 she1323. All rights reserved.
//  兑换记录

#import "YLExchangeRecordController.h"
#import "YLExchangeRecordTableViewCell.h"
#import "YLExchangeRecordHeaderView.h"
#import "YLExchangeRecordViewModel.h"
#import "YLExchangeRecordModel.h"
@interface YLExchangeRecordController ()
@property (nonatomic, strong) YLExchangeRecordViewModel *viewModel;
@end

@implementation YLExchangeRecordController

- (YLExchangeRecordViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLExchangeRecordViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"兑换记录";
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLExchangeRecordTableViewCell class]) bundle:nil] forCellReuseIdentifier:[YLExchangeRecordTableViewCell cellID]];
    __weak typeof(self) weakSelf = self;
    [[self.viewModel.requestCommand execute:nil] subscribeNext:^(id  _Nullable x) {
        [weakSelf setEmpty];
        [weakSelf.tableView reloadData];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    YLExchangeRecordItem *item = self.viewModel.group.httpResult[section];
    return item.pointMonthHistoryDetail.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.viewModel.group.httpResult.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLExchangeRecordItem *item = self.viewModel.group.httpResult[indexPath.section];
    YLExchangeRecordModel *model = item.pointMonthHistoryDetail[indexPath.row];
    YLExchangeRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLExchangeRecordTableViewCell cellID]];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    YLExchangeRecordItem *item = self.viewModel.group.httpResult[section];
    if (item.pointMonthHistoryDetail.count == 0) {
        return 0;
    }
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    YLExchangeRecordItem *item = self.viewModel.group.httpResult[section];
    if (item.pointMonthHistoryDetail.count == 0) {
        return 0;
    }
    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    YLExchangeRecordHeaderView *header = [[YLExchangeRecordHeaderView alloc] init];
    YLExchangeRecordItem *item = self.viewModel.group.httpResult[section];
    header.showTitle = item.monthHistoryName;
    header.totalNeetPoint = item.totalPointAmount;
    return header;
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString * text = @"抱歉,暂时还没有积分兑换记录!";
    
    NSDictionary * attributes = @ { NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0f ],
        NSForegroundColorAttributeName:YLColor(130, 130, 130)};
    
    return [[ NSAttributedString  alloc ] initWithString:text attributes:attributes];
}


@end
